package marmalius;

import java.util.ArrayList;

import marmalius.player.Item;

public class Mining {
	
	private ArrayList<MineInfo> mines;
	
	public Mining () {
		mines = new ArrayList<MineInfo>();
	}
	
	public void process () {
		for (int i = 0; i < mines.size(); i++) {
			MineInfo tmp = mines.get(i);
			if (tmp.life <= 0) {
				if (tmp.respawn > 0) {
					mines.get(i).respawn--;
				}
				else {
					//TODO mettre a jour l'affichage de la mine (present)
					mines.remove(i);
				}
			}
		}
	}
	
	public void ResetMining (client player) {
		player.mineId = 0;
		player.mineX = 0;
		player.mineY = 0;
		player.mineTimer = 0;
		player.mTimer = 0;
		player.resetAnimation();
		player.resetanim = 4;
	}
	
	public void updateMiningTimer (client player) {
		if (player.mTimer > 0) {
			player.mTimer--;
		}
	}
	
	//TODO
	public void updateMining (client player) {
		if (player.mTimer <= 0) {
			if (playerHavePickAxe(player) > 0) {
				MineInfo mine = getMineInfo(player.mineId, player.mineX, player.mineY);
				player.addSkillXP(mine.xp, 14);
				if (mine.life > 0 && player.addItem(mine.ore, 1)) {
					player.sendMessage("You get some ore.");
					mine.life--;
					if (mine.life > 0) {
						player.mTimer = player.mineTimer;
						player.ContinuedAnim(625); //TODO not good
					}
					else {
						//TODO mettre a jour l'affichage de la mine (vide)
						ResetMining(player);
					}
				}
				else {
					ResetMining(player);
				}
			}
			else {
				player.sendMessage("You need a pick Axe to this mine.");
				ResetMining(player);
			}
		}
	}
		
	public boolean checkMining (int mineId, client player, int x, int y) {
		MineInfo mine = getMineInfo(mineId, x, y);
		if (mine.mineId == 0) {
			return false;
		}
		int pickAxeId = playerHavePickAxe(player);
		if (pickAxeId > 0) {
			PickAxeInfo pickAxe = getPickAxeInfo(pickAxeId, player);
			if (player.mTimer <= 0) {
				if (player.playerLevel[14] >= mine.lvlReq) {
					if (mine.life > 0) {
						player.ContinuedAnim(pickAxe.anim);
						player.resetanim = 999;
						player.sendMessage(mine.oreName);
						player.mineX = x;
						player.mineY = y;
						player.mineTimer = GetMiningMineTime(mineId, player.playerLevel[14]); //TODO ??? pickAxe.bestTime
						player.mTimer = player.mineTimer;
						player.updateRequired = true;
						player.appearanceUpdateRequired = true;
						return true;
					}
					else {
						player.sendMessage("Cette mine est vide.");
					}
				}
				else  {
					player.sendMessage("You need a mining level of " + mine.lvlReq + ".");
				}
			}
		}
		else {
			player.sendMessage("You need an pickaxe.");
			ResetMining(player);
		}
		return false;
	}
	
	//TODO
	private int GetMiningMineTime (int mineId, int MiningLevel) {
		int lvlB = MiningLevel / 4;
		
		switch (mineId) {
			case 2095: // silver ?
				return 40 - lvlB;
		}
		return 5;
	}
		
	private MineInfo getMineInfo (int mineId, int x, int y) {
		for (int i = 0; i < mines.size(); i++) {
			MineInfo tmp = mines.get(i);
			if (tmp.mineId == mineId && tmp.x == x && tmp.y == y) {
				return mines.get(i);
			}
		}
		//empty mine : 453,452,451,450
		MineInfo mine = new MineInfo();
		switch (mineId) {
			case 2090: // Copper ore
			case 2091:
				mine.mineId = 2091;
				mine.lvlReq = 1;
				mine.xp = 17;
				mine.ore = 436;
				mine.oreName = "copper ore";
				mine.life = 1;
				mine.respawn = 5;
				break;
			case 2092: // Iron ore
		    case 2093:
		    	mine.mineId = 2093;
				mine.lvlReq = 15;
				mine.xp = 35;
				mine.ore = 440;
				mine.oreName = "iron ore";
				mine.life = 1;
				mine.respawn = 5;
				break;
		    case 2096: // Coal
		    case 2097:
		    	mine.mineId = 2097;
				mine.lvlReq = 40;
				mine.xp = 50;
				mine.ore = 453;
				mine.oreName = "coal";
				mine.life = 1;
				mine.respawn = 5;
				break;
			
		    case 2102: // Mithril ore
	        case 2103:
	            mine.mineId = 2103;
				mine.lvlReq = 55;
				mine.xp = 80;
				mine.ore = 447;
				mine.oreName = "mithril ore";
				mine.life = 1;
				mine.respawn = 5;
				break;
	        case 2104: // Adamantite ore
	        case 2105:
	            mine.mineId = 2105;
				mine.lvlReq = 70;
				mine.xp = 95;
				mine.ore = 449;
				mine.oreName = "adamantite ore";
				mine.life = 1;
				mine.respawn = 5;
				break;
	        case 2106: // Runite ore
	        case 2107:
	        	mine.mineId = 2107;
				mine.lvlReq = 85;
				mine.xp = 125;
				mine.ore = 451;
				mine.oreName = "runite ore";
				mine.life = 1;
				mine.respawn = 5;
				break; /*
	        case 2111: // mining elemental(dragon) ore, seventh rock type [6]
	            
	        case 2108:
	        case 2109: // mining elemental(dragon) ore, seventh rock type [6]
	           
			case 2095:
				mine.mineId = 2095;
				mine.lvlReq = 20;
				mine.xp = 40;
				mine.ore = 0;
				mine.oreName = "";
				mine.life = 1;
				mine.respawn = 5;
				break;
				*/
			default:
				mine.mineId = 0;
				mine.x = 0;
				mine.y = 0;
				mine.lvlReq = 0;
				mine.xp = 0;
				mine.ore = 0;
				mine.oreName = "";
		}
		mines.add(mine);
		return mine;
	}
		
	private class MineInfo {
		public int mineId;
		public int x;
		public int y;
		public int lvlReq;
		public int xp;
		public int ore;
		public int life;
		public int respawn;
		public String oreName;
	}
	
	private PickAxeInfo getPickAxeInfo (int pickAxeId, client player) {
		PickAxeInfo pickAxe = new PickAxeInfo();
		switch (pickAxeId) {
			case 1265:
				pickAxe.pickAxeId = 1265;
				pickAxe.anim = 625;
				pickAxe.bestTime = 40 - (0.1 * player.getLevelForXP(player.playerXP[14]));
				break;
			case 1267:
				pickAxe.pickAxeId = 1267;
				pickAxe.anim = 626;
				pickAxe.bestTime = 38 - (0.1 * player.getLevelForXP(player.playerXP[14]));
			case 1269:
				pickAxe.pickAxeId = 1269;
				pickAxe.anim = 627;
				pickAxe.bestTime = 36 - (0.1 * player.getLevelForXP(player.playerXP[14]));
			case 1273:
				pickAxe.pickAxeId = 1273;
				pickAxe.anim = 629;
				pickAxe.bestTime = 34 - (0.1 * player.getLevelForXP(player.playerXP[14]));
			case 1271:
				pickAxe.pickAxeId = 1271;
				pickAxe.anim = 628;
				pickAxe.bestTime = 32 - (0.1 * player.getLevelForXP(player.playerXP[14]));
			case 1275:
				pickAxe.pickAxeId = 1275;
				pickAxe.anim = 624;
				pickAxe.bestTime = 30 - (0.1 * player.getLevelForXP(player.playerXP[14]));
			default:
				pickAxe.pickAxeId = 0;
				pickAxe.anim = 0;
				pickAxe.bestTime = 0;
		}
		return pickAxe;
	}
	
	public class PickAxeInfo {
		public int pickAxeId;
		public int anim;
		public double bestTime;
	}
		
	public int playerHavePickAxe (client player) {
		Item pickaxe = player.getInventory().getPickAxe();
		if (pickaxe != null)
			return pickaxe.getItemTemplate().getId();
		else
			return 0;
		/*
		int [] itemPickAxe = new int[6];
		itemPickAxe[0] = 1265; // bronze pick
		itemPickAxe[1] = 1267; // iron pick
		itemPickAxe[2] = 1269; // steel pick
		itemPickAxe[3] = 1273; // mith pick
		itemPickAxe[4] = 1271; // addy pick
		itemPickAxe[5] = 1275; // rune pick
		for (int j = 0; j < itemPickAxe.length; j++) {
			for (int i = 0; i < player.playerItems.length; i++) {
				if (player.playerItems[i] == itemPickAxe[j] + 1) {
					return itemPickAxe[j];
				}
			}
			for (int i2 = 0; i2 < player.playerEquipment.length; i2++) {
				if (player.playerEquipment[i2] == itemPickAxe[j]) {
					return itemPickAxe[j];
				}
			}
		}
		return 0;
		*/
	}
	
	/*
	
	// ----tj007razor: variables for mining
	public int[] rockTimer = new int[7]; // 7 types of rocks this will allow mining for
	public int[] rockX = new int[7]; // 7 different rocks can be used at the same time by players
	public int[] rockY = new int[7]; // 7 different rocks can be used at the same time by players
	public int stunTimer = 0; // stunning players after random
	public int rockExplode = 0; // random explosion
	public boolean hasPick = false; // does the player have a pick
	public int bestTime = 0; // best time you can have when mining
	public int mineAnim = 0; // animation for mining
	public int barID = 0; // id of the bar that will be given
	public int mineXP = 0; // xp to be given from mining
	public int[] rockID = new int[7]; // id of the rock object being mined

	// checks for picks and set animation
	public void checkPick(int i) {
		double bestTimez = 0;

		if (playerEquipment[playerWeapon] == 1265) { // bronze pick
			mineAnim = 625;
			hasPick = true;
			bestTimez = 40 - (0.1 * getLevelForXP(playerXP[14]));
		} else if (playerEquipment[playerWeapon] == 1267) { // iron pick
			mineAnim = 626;
			hasPick = true;
			bestTimez = 38 - (0.1 * getLevelForXP(playerXP[14]));
		} else if (playerEquipment[playerWeapon] == 1269) { // steel pick
			mineAnim = 627;
			hasPick = true;
			bestTimez = 36 - (0.1 * getLevelForXP(playerXP[14]));
		} else if (playerEquipment[playerWeapon] == 1273) { // mith pick
			mineAnim = 629;
			hasPick = true;
			bestTimez = 34 - (0.1 * getLevelForXP(playerXP[14]));
		} else if (playerEquipment[playerWeapon] == 1271) { // addy pick
			mineAnim = 628;
			hasPick = true;
			bestTimez = 32 - (0.1 * getLevelForXP(playerXP[14]));
		} else if (playerEquipment[playerWeapon] == 1275) { // rune pick
			mineAnim = 624;
			hasPick = true;
			bestTimez = 30 - (0.1 * getLevelForXP(playerXP[14]));
		}
		bestTimez += .5;
		bestTime = (int) bestTimez;
		bestTime = misc.random(bestTime) + i * 2 + 1;
		if (bestTime <= 7) {
			bestTime = 8;
		}
	}

	// begins the mining process, and sets up variables
	public void startMine(int i, int req, int barIDz, int multXP, int rockid, int objectX, int objectY) {
		checkPick(i);
		if (rockTimer[i] == 0) {
			if (stunTimer == 0) {
				if (playerLevel[14] >= req) {
					if (hasPick) {
						rockX[i] = objectX;
						rockY[i] = objectY;
						rockTimer[i] = bestTime;
						setAnimation(mineAnim);
						barID = barIDz;
						mineXP = multXP * playerLevel[14];
						rockID[i] = rockid;
					} else {
						sendMessage(
								"You need a pick axe equiped to mine this rock.");
					}
				} else {
					sendMessage(
							"You need level " + req
							+ " or better mining to mine this rock.");
				}
			} else {
				sendMessage("You are stunned!");
				rockTimer[i] = 10;
			}
		} else {
			sendMessage("You start mining!");
			rockTimer[i] = 10;
		}
	}

	public void doMine(int i) {
		// process for mining rocks
		
		// makes the animation for mining keep going
		if ((rockTimer[i] == 35) || (rockTimer[i] == 30) || (rockTimer[i] == 25)
				|| (rockTimer[i] == 20) || (rockTimer[i] == 15)
				|| (rockTimer[i] == 10) || (rockTimer[i] == 5)) {
			setAnimation(mineAnim);
		}
	
		// timer for mining/replacing the object
		if (rockTimer[i] > 0) {
			rockTimer[i]--;
		} else {
			rockTimer[i] = 0;
		}
	
		// tj007razor: random occasional rock explosion
		if (rockTimer[i] > 7) {
			rockExplode = misc.random(300);
		}
		if (rockExplode == 1) {
			stillgfx(189, rockY[i], rockX[i]);
			rockTimer[i] = 0;
			sendMessage("The rock explodes and forces you to stop mining!");
			setAnimation(0x328);
			rockExplode = 0;
			stillgfx(80, absY, absX);
			EntangleDelay = 30;
			stunTimer = 30;
		}

		// adds the bar and replaces the object
		if (rockTimer[i] == 7) {
			addItem(barID, 1);
			setAnimation(0x328);
			addSkillXP(mineXP, 14);
			ReplaceObject2(rockX[i], rockY[i], 450, -2, 10);
		} else if (rockTimer[i] == 1) {
			ReplaceObject2(rockX[i], rockY[i], rockID[i], -2, 10);
		}
	}
	*/
}
